package mc.web;

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import mc.bus.BazaUslug;
import mc.domain.Adres;
import mc.domain.Pacjent;
import mc.domain.Usluga;
import mc.util.MedcenterRequestUtil;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.propertyeditors.CustomNumberEditor;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

public class UslugaForm extends SimpleFormController {
	
	private final Log logger = LogFactory.getLog(getClass());	
	BazaUslug bazauslug;
			



	@Override
	protected Map referenceData(HttpServletRequest arg0) throws Exception {
		
		Map<Object,Object> referencjeData = new HashMap<Object, Object>();
		
		referencjeData.put("uslugi", bazauslug.getAllUslugi());
		return referencjeData;
	}
	
	
	@Override
	protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
		
		DecimalFormat decimalFormat = new DecimalFormat();
																	//ustawiam separator dziesietny
		DecimalFormatSymbols symbols = new DecimalFormatSymbols();
		symbols.setDecimalSeparator(','); 
		decimalFormat.setDecimalFormatSymbols(symbols);
		decimalFormat.setGroupingUsed(false);
																	//ustawiam ilosc cyfr w czesci ulamkowej	
		decimalFormat.setMaximumFractionDigits(2);
		decimalFormat.setMinimumFractionDigits(2);
		
		binder.registerCustomEditor(Double.class,new CustomNumberEditor(Double.class, decimalFormat, true));
		binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
		
	}


	@Override
	protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		
		
		ModelAndView mv;
		
		HashMap<String,Integer> h=MedcenterRequestUtil.checkButtonName(request);
		
		switch(h.get("param")){
		
		case MedcenterRequestUtil.DEL:{
			
			Map<String,Object> model= errors.getModel();
			
			bazauslug.deleteUsluga(h.get("id").intValue());
			model.clear();
			model.put("uslugi", bazauslug.getAllUslugi());
			model.put("usluga", new Usluga());
			

			return new ModelAndView(getSuccessView(),model);
				
			
		}
		case MedcenterRequestUtil.EDIT:{
			
			Map<String,Object> model= errors.getModel();
			
			Usluga u=new Usluga();
			u=bazauslug.getUslugaById(h.get("id").intValue());
			model.clear();
			model.put("uslugi", bazauslug.getAllUslugi());
			model.put("usluga", u);
			
			
			return new ModelAndView(getSuccessView(),model);
			
			
		}
		
		
		case MedcenterRequestUtil.SAVE:{
		
			Map<Object,Object> model= errors.getModel();
			
			Usluga u=(Usluga)model.get("usluga");
			bazauslug.saveUsluga(u);
			model.clear();
			model.put("uslugi", bazauslug.getAllUslugi());
			model.put("usluga", new Usluga());
			
			return new ModelAndView(getSuccessView(),model);
			
		}
		default:{
			logger.info("Nieprawidlowe zadanie");
			
			mv=new ModelAndView("error");
			
			return mv;
		}
		
		}
		
		
	}
	
	@Override
	protected boolean suppressValidation(HttpServletRequest request,
			Object command) {
		
		HashMap<String,Integer> h=MedcenterRequestUtil.checkButtonName(request);
		
		logger.info("metoda validate");
		
		switch(h.get("param")){
		
		case MedcenterRequestUtil.SAVE:{
			
			logger.info("metoda validate po switchu");
			return false;
			
		}
		default:{
			logger.info("validator pominiety");
			return true;
		}
		
		}
		
		
	}
	
	



	public BazaUslug getBazauslug() {
		return bazauslug;
	}


	public void setBazauslug(BazaUslug bazauslug) {
		this.bazauslug = bazauslug;
	}





	
	
	
	
	
	
	
}
